package com.gxw.kotlint.ui.activity

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.InputType
import android.text.method.DigitsKeyListener
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.gxw.kotlint.R
import com.gxw.kotlint.utils.StringsUtil
import com.gxw.kotlint.utils.ToastUtil
import org.jetbrains.anko.*

class ThirdActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        var loginId: EditText = EditText(this@ThirdActivity);
        var pwd: EditText = EditText(this@ThirdActivity);
        relativeLayout {
            backgroundColor = Color.parseColor("#f4f4f4")

            linearLayout {
                orientation = LinearLayout.VERTICAL

                imageView { //顶部logo
                    id = R.id.img_login_logo
                    imageResource = R.mipmap.pic_login_logo
                }.lparams(width = dip(90), height = dip(57)) {
                    topMargin = dip(58)
                    gravity = Gravity.CENTER_HORIZONTAL
                }

                view { //线
                    id = R.id.view_include1
                    backgroundColor = Color.parseColor("#d8d8d8")
                }.lparams (width = dip(28), height = dp_i(1)) {
                    gravity = Gravity.CENTER_HORIZONTAL
                }

                textView (R.string.tv_login_logo_top) { //也可以是textView("hello world")
                    textColor = Color.parseColor("#6c6c6c")
                    //为什么sp(10)或sp(10F)报错
                    //  var fontSize = 10f
                    //  textSize = px2sp(10)小，不对
                    //默认是sp
                    textSize = 10F

                }.lparams (width = wrapContent, height = wrapContent) {
                    topMargin = dip(11)
                    gravity = Gravity.CENTER_HORIZONTAL
                }

                view {

                    backgroundColor = getColor(R.color.line_color)

                }.lparams (width = matchParent, height = dp_i(1)) {
                    //不能这么写dip(R.dimen.mag_default_l)
                    topMargin = dip(18)
                }

                linearLayout {
                    orientation = LinearLayout.VERTICAL
                    backgroundColor = Color.WHITE
                    leftPadding = dip(16)

                    linearLayout {
                        orientation = LinearLayout.HORIZONTAL
                        imageView(R.drawable.shape_login_circle).lparams {
                            gravity = Gravity.CENTER
                            leftMargin = dip(8)
                        }

                        loginId = editText {
                            id = R.id.et_login_loginid
                            hint = getString(R.string.et_login_loginid)
                            background = null
                            //padding在这
                            textSize = 16F
                        }.lparams (width = matchParent, height = matchParent) {
                            //margin在这
                            leftMargin = dip(8)
                        }


                    }.lparams(height = dip(48), width = matchParent)

                    view {
                        backgroundColor = getColor(R.color.line_color)
                    }.lparams(width = matchParent, height = dp_i(1)) {
                        leftMargin = dip(16)
                        rightMargin = dip(16)
                    }

                    linearLayout {
                        orientation = LinearLayout.HORIZONTAL
                        imageView(R.drawable.shape_login_circle).lparams {
                            gravity = Gravity.CENTER
                            leftMargin = dip(8)
                        }

                        var imgSee: ImageView = ImageView(this@ThirdActivity)

                        //margin在这
                        pwd = editText {
                            id = R.id.et_login_pwd
                            //                            hint = getString(R.string.et_login_pwd)
                            hintResource = R.string.et_login_pwd
                            background = null
                            //padding在这
                            textSize = 16F
                            //设置输入类型
                            //                            setInputExtras(InputType.TYPE_TEXT_VARIATION_PASSWORD)
                            inputType = InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD//不管用
                            //密码不可见
                            transformationMethod = PasswordTransformationMethod.getInstance()
                            //                            //设置输入的文字限定
                            keyListener = DigitsKeyListener.getInstance("0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
                            //                            setInputExtras(InputType.TYPE_TEXT_VARIATION_PASSWORD)
                            var lp = RelativeLayout.LayoutParams(matchParent, matchParent)
                            lp.addRule(RelativeLayout.RIGHT_OF, imgSee.id)
                            layoutParams = lp
                        }

                        imgSee = imageView (R.mipmap.ic_login_see) {
                            id = R.id.img_login_seepwd
                            onClick {
                                pwd.transformationMethod = HideReturnsTransformationMethod.getInstance()//密码可见
                            }
                        }.lparams(width = wrapContent, height = wrapContent) {
                            gravity = Gravity.CENTER_VERTICAL.and(Gravity.RIGHT)
                            rightMargin = dip(6)
                        }
                    }.lparams(height = dip(48), width = matchParent)
                }.lparams(width = matchParent, height = dip(96))

                view {
                    backgroundColor = getColor(R.color.line_color)
                }.lparams(width = matchParent, height = dp_i(1))

                button(R.string.btn_login_submit) { //提交按钮
                    textColor = Color.WHITE
                    backgroundResource = R.drawable.selector_login_subbtn_bg
                    onClick {
                        var loginName: String = loginId.text.toString()
                        var password: String = pwd.text.toString()

                        var stringUtil = StringsUtil()

                        var nameIsEmpty:Boolean = stringUtil.strIsEmpty(loginName)//用戶名是否為空
                        var pwdIsEmpty:Boolean = stringUtil.strIsEmpty(password)//密碼是否為空

                        if(nameIsEmpty||pwdIsEmpty){//判斷用戶名密碼是否為空
                            var toastUtil = ToastUtil()
                            toastUtil.toastCenter(R.string.et_login_pwd,this@ThirdActivity)
                        }else{



                        }

                        var intent = Intent()
                        intent.setClass(this@ThirdActivity, ForthActivity::class.java)
                        intent.putExtra("login", loginName)
                        intent.putExtra("password", password)
                        startActivity(intent)

                    }
                }.lparams(width = matchParent, height = dip(46)) {
                    margin = dip(16)
                }


            }.lparams(width = matchParent, height = matchParent)

        }


    }

    /**
     * dp转px
     *
     * 从Float转 最后返回Float
     */
    fun View.dp_f(dp: Float): Float {
        return TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dp, context.resources.displayMetrics)
    }

    /**
     * dp转px
     *
     * 从int转换 最后返回int
     */
    fun View.dp_i(dp: Int): Int {
        return dp_f(dp.toFloat()).toInt()
    }


}
